Range (computer programming)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
top
In computer science, the term range may refer to one of three things:
1. The possible values that may be stored in a variable.
2. The upper and lower bounds of an array.
3. An alternative to iterator.
Contents
• See also
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Range of a variable
The range of a variable is given as the set of possible values that that variable can hold. In the case of an integer, the variable definition is restricted to whole numbers only, and the range will cover every number within its range (including the maximum and minimum). For example, the range of a signed 16-bit integer variable is all the integers from −32,768 to +32,767.
Range of an array
When an array is numerically indexed, its range is the upper and lower bound of the array. Depending on the environment, a warning, a fatal exception, or unpredictable behavior will occur if the program attempts to access an array element that is outside the range. In some programming languages, such as C, arrays have a fixed lower bound (zero) and will contain data at each position up to the upper bound (so an array with 5 elements will have a range of 0 to 4). In others, such as PHP, an array may have holes where no element is defined, and therefore an array with a range of 0 to 4 will have up to 5 elements (and a minimum of 2).
Range as an alternative to iterator
Another meaning of range in computer science is an alternative to iterator. When used in this sense, range is defined as "a pair of begin/end iterators packed together".cite-ref-itersmustgo-1-0[1] It is argued cite-ref-itersmustgo-1-1[1] that "Ranges are a superior abstraction" (compared to iterators) for several reasons, including better safety.
Range as a data type
A data type for ranges can be implemented using generics.
Example in C#.
public record Range<T>(T Start, T End) where T : IComparable;
Example in Kotlin.
data class Range<T: Comparable<T>>(val start: T, val end: T)
Example in PHP.
readonly class Range<T> {
public function __construct(
public T $start,
public T $end,
) {}
}
Example in Python.
from dataclasses import dataclass
@dataclass
class Range[T]:
start: T
end: T
Range as a operator
Rust has the .. and ..= operators.
let heartwarming = "heartwarming!".to_string();
let warm = &heartwarming[5..9];
Zig also has the .. operator.
// To iterate over consecutive integers, use the range syntax.
var sum: usize = 0;
for (0..5) |i| {
sum += i;
}
string items[] = ["one","two","three","four"];
string first_three_items[] = items[0..2];
[1..4]
// Outputs: [1; 2; 3; 4]
for (i in 1..5) print(i)
for( 1..5) { print }
See also
References
cite-note-itersmustgo-11. ↑ citerefandrei-alexandrescu2009Andrei Alexandrescu (6 May 2009). "Iterators Must Go" (PDF). BoostCon 2009. Retrieved 29 July 2014.
cite-note-22. ↑ cppreference
cite-note-33. ↑ Boost.Range documentation
cite-note-44. ↑ D Phobos Runtime Library std.range module
cite-note-55. ↑ "Range in std::ops - Rust". doc.rust-lang.org. Retrieved 17 October 2024.
cite-note-66. ↑ citerefbillwagner2023BillWagner (14 November 2023). "Explore ranges of data using indices and ranges - C#". learn.microsoft.com. Retrieved 2025-02-22.
cite-note-77. ↑ "Range Operator". F# by example. 17 February 2023. Retrieved 2025-02-22.
cite-note-88. ↑ "Ranges and progressions - Kotlin". Kotlin Help. Retrieved 2025-02-22.
cite-note-99. ↑ "perlop - Perl expressions: operators, precedence, string literals - Perldoc Browser". perldoc.perl.org. Retrieved 2025-02-22.
cite-note-1010. ↑ "Built-in Functions". Python documentation. Python Software Foundation. Retrieved 17 December 2024.
cite-note-1111. ↑ "PHP: range - Manual". The PHP Documentation Group. Retrieved 17 December 2024.